package NIO;

import lombok.Data;

import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.concurrent.atomic.AtomicInteger;

public class PrintAllFile {
    public static void main(String[] args) throws IOException {
        printDir();
        System.out.println("<---------------------------------->");
        printJavaFile();
    }

    public static void printDir()throws IOException{
        AtomicInteger dirCount = new AtomicInteger(0);
        AtomicInteger fileCount = new AtomicInteger(0);
        Path path= Paths.get("D:/代码2.txt/javaSE/javaSE");
        Files.walkFileTree(path, new SimpleFileVisitor<>() {
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                System.out.println(dir.normalize());
                dirCount.incrementAndGet();
                return super.preVisitDirectory(dir, attrs);
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                System.out.println(file.normalize());
                fileCount.incrementAndGet();
                return super.visitFile(file, attrs);
            }
        });
        System.out.println(fileCount.get());
        System.out.println(dirCount.get());
    }

    public static void printJavaFile() throws IOException {
        AtomicInteger fileCount = new AtomicInteger(0);
        Path sourcePath=Paths.get("D:/代码2.txt/javaSE/javaSE");
        Files.walkFileTree(sourcePath,new SimpleFileVisitor<>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                if(file.getFileName().toString().endsWith(".java")){
                    fileCount.incrementAndGet();
                    System.out.println(file.normalize());
                }
                return super.visitFile(file, attrs);
            }
        });
        System.out.println(fileCount.get());
    }
}
